package com.kancy.qrshare.common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

/**
 * 系统设置
 * @author kancy
 *
 */
public class Setting {

	/**
	 * 是否显示图片和网页资源
	 */
	public static boolean show_static_resource = true;
	/**
	 * 采用固定的端口设置
	 */
	public static int http_port = 0;
	/**
	 * 线程池大小设置，同时下载最大数量控制
	 */
	public static int http_pool_size = 0;
	/**
	 * 延迟自动关闭系统，时间单位毫秒
	 */
	public static long wait_delay_time = 1L;
	/**
	 * 默认编码
	 */
	public static String default_charset = Constants.CHARACTER_UTF8;
	/**
	 * content_disposition编码
	 */
	public static String content_disposition_charset = Constants.CHARACTER_ISO8859_1;

	//----------------------------------------------------------------------------
	// setting end
	//-----------------------------------------------------------------------------

	private static String configpath = System.getProperty("user.home").replace("\\", "/").concat("/").concat("QrShare/config.ini");

	private volatile static Properties setting = new Properties();

	public static String getProperty(String key) {
		return setting.getProperty(key);
	}
	public static String getProperty(String key, String defaultValue) {
		return setting.getProperty(key, defaultValue);
	}
	public static int getProperty(String key, int defaultValue) {
		return Integer.parseInt(setting.getProperty(key, String.valueOf(defaultValue)));
	}
	public static long getProperty(String key, long defaultValue) {
		return Long.parseLong(setting.getProperty(key, String.valueOf(defaultValue)));
	}
	public static boolean getProperty(String key, boolean defaultValue) {
		return "true".equalsIgnoreCase(setting.getProperty(key, String.valueOf(defaultValue)));
	}
	public static void setProperty(String key, String value) {
		setting.setProperty(key, value);
		storeSetting();
	}

	private static void storeSetting() {
		OutputStream fos = null;
		try {
			fos = new FileOutputStream(configpath);// 加载读取文件流
			setting.store(fos, "QrShare Config");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	/**
	 * 初始化设置
	 */
	static{
		init();
		// 取值
		show_static_resource = getProperty("show_static_resource", show_static_resource);
		wait_delay_time = getProperty("wait_delay_time", wait_delay_time);
		http_port = getProperty("http_port", http_port);
		http_pool_size = getProperty("http_pool_size", http_pool_size);
		default_charset = getProperty("default_charset", default_charset);
		content_disposition_charset = getProperty("content_disposition_charset", content_disposition_charset);
	}

	private static void initDefaultConfig() {
		setting.setProperty("default_charset", default_charset);
		setting.setProperty("wait_delay_time", String.valueOf(wait_delay_time));
		setting.setProperty("http_port", String.valueOf(http_port));
		setting.setProperty("http_pool_size", String.valueOf(http_pool_size));
		setting.setProperty("show_static_resource", String.valueOf(show_static_resource));
		setting.setProperty("content_disposition_charset", content_disposition_charset);
		storeSetting();
	}

	private static void init() {
		FileInputStream fis = null;
		try {
			File file = new File(configpath);
			if(!file.getParentFile().exists() && !file.getParentFile().isDirectory()){
				file.getParentFile().mkdirs();
			}
			if (!(file.exists() && file.isFile())) {
				file.createNewFile();
				initDefaultConfig();
			}
			fis = new FileInputStream(configpath);
			setting.load(fis);
		} catch (Exception e) {
			e.printStackTrace();
		}  finally {
			if (fis != null) {
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}
